package com.sjm.suchbear.easymap.Util;

import android.app.Dialog;
import android.content.Context;
import android.os.Looper;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.widget.RelativeLayout;

import com.sjm.suchbear.easymap.R;

/**
 * Created by Suchbear on 2018/3/12.
 */

public class DialogLoading {


    public static Dialog m_LodingDialog=null;
    public static Context m_context;
    public static int m_count = 0;


    public DialogLoading(Context context) {
        m_context = context;
        m_count=0;
        m_LodingDialog=null;
    }


    public static void StartLodingDialog(Context context) {

        m_context= context;

        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Looper.prepare();
                    loading();
                    Looper.loop();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }


    public static void loading(){
        Log.e("m_count", String.valueOf(m_count));
        try {
            if(m_count!=0){
                return;
            }
            Log.e("m_count", String.valueOf(m_count));
            m_count++;
            if (m_count == 1) {
                m_LodingDialog = new Dialog(m_context, R.style.dialog_style);
                m_LodingDialog.setContentView(R.layout.dialog_loding_all);
                m_LodingDialog.setCanceledOnTouchOutside(true);
                Window win = m_LodingDialog.getWindow();
                win.setGravity(Gravity.CENTER);
                win.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                m_LodingDialog.setCanceledOnTouchOutside(false);
                Log.e("m_count",String.valueOf(m_count));
                if (m_LodingDialog!=null)
                {
                    m_LodingDialog.show();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    public static void StopLodingDialog() {
        try {
            if(m_count!=0)
            {
                m_count=0;
            }else{
                return;
            }
            if (m_count == 0) {
                if(m_LodingDialog!=null){
                    m_LodingDialog.dismiss();}
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
